package logic.netSerer.GUI.nettyServer;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import logic.util.EasyLog;

public class NettyClientHandler extends ChannelHandlerAdapter {

	public int index=0;
	
	public NettyClientHandler(int index) {
		this.index=index;
	}
	
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
		EasyLog.info(getClass(), "客户端:{} 收到消息:{}", index,msg);
		if(msg.toString().equals("quit")){
			ctx.channel().close();
		}else{
			ctx.writeAndFlush("msg from client");
		}
	}
	
	public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise channelPromise) throws Exception{
		EasyLog.info(getClass(), "客户端:{} 发送消息:{}", index,msg);
	}
	
	public void disconnect(ChannelHandlerContext ctx,ChannelPromise channelPromise) throws Exception{
		EasyLog.info(getClass(), "客户端:{} 断开连接", index);
	}
	public void channelActive(ChannelHandlerContext ctx) throws Exception{
		EasyLog.info(getClass(), "客户端{} 连接上了.", index);
		ctx.writeAndFlush("客户端发送的消息,服务端收到了么?");
	}
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		EasyLog.error(getClass(), cause);
	}

	
	
	
}
